<?php

/*****************************************************

  Copyright:	(c) 2006  SeaDesign Web Studio
        Web:	www.seadesign.ru
      Email: 	seadesign@gmail.com
  
*****************************************************/

require_once('db.inc');

/***************************************************************************

	Split records by pages and render page numbers and navigation

***************************************************************************/
class PageFormatter 
{
	function PageFormatter($recCount, $recOnPage, $pageNumsVisible) 
	{
		$this->recCount 			= $recCount;
		$this->recOnPage 			= $recOnPage;
		$this->pageCount 			= ceil($this->recCount/$this->recOnPage);
		$this->pageNumsVisible		= $pageNumsVisible;
		$this->isPagesLooped		= true;
	}
	
	function create() 
	{
		$this->currPage = 1;				//1-based number
		global $par;
		if ($par->exist('page')) 
		{
			$this->currPage = $par->get('page');
		}
		
		$this->prevPageNum = $this->currPage-1;
		if (!$this->prevPageNum) 
		{
			if ($this->isPagesLooped) {
				$this->prevPageNum = $this->pageCount;
			} 
			else {
				$this->prevPageNum = 1;
			}
		}
		
		$this->nextPageNum = $this->currPage+1;
		if ($this->nextPageNum > $this->pageCount) {
			if ($this->isPagesLooped) {
				$this->nextPageNum = 1;
			}
			else {
				$this->nextPageNum = $this->pageCount;
			}
		}
		

		$pageNumsBefore = floor(($this->pageNumsVisible-1)/2);		
		$pageNumsAfter = ceil(($this->pageNumsVisible-1)/2);
		if (($this->pageCount - $this->currPage) < $pageNumsAfter) {
			$pageNumsBefore += ($pageNumsAfter - ($this->pageCount - $this->currPage));
		}
		
		$pageNum = ($this->currPage - $pageNumsBefore) <=0 ? 
							1 : 
							($this->currPage - $pageNumsBefore);
		$count = 1;
		for (; $pageNum<$this->currPage; ++$pageNum, ++$count) {
			$this->visiblePageNumbers[] = $pageNum;
		}
		
		$this->visiblePageNumbers[] = $this->currPage;
		++$pageNum;
		
		$pageNumsAfter = $this->pageNumsVisible-$count;
		for (	$count=1; 
				$count<=$pageNumsAfter && $pageNum<=$this->pageCount; 
				++$pageNum, ++$count) {
			$this->visiblePageNumbers[] = $pageNum;
		}
	}
	
	function getStartRec() {								//$q->startFrom = $fmter->getStartRec();
		if ($this->currPage > 1) {
			return ($this->currPage-1) * $this->recOnPage;
		}
		return 0;
	}
	
	function getRecOnPage() 
	{
		return $this->recOnPage;
	}
	
	function isOnFirst() 
	{
		return $this->currPage == 1;
	}
	
	function isOnLast() 
	{
		return $this->currPage == $this->pageCount;
	}
	
	function prevPage() 
	{
		return $this->prevPageNum;
	}
	
	function nextPage() 
	{
		return $this->nextPageNum;
	}
	
	function lastPage() 
	{
		return $this->pageCount;
	}
	
	function pageCount() 
	{
		return $this->pageCount;
	}
	
	function recCount() 
	{
		return $this->recCount;
	}
	
	function currentPage() 
	{
		return $this->currPage;
	}
	
	function visiblePages() 
	{
		return $this->visiblePageNumbers;
	}
	
	///////////////////////////////	
	/*
	{FIRST} = 1
	{PREVIOUS} = prevPage()
	{PAGE_NUMBERS} = visiblePages()
	{NEXT} = nextPage()
	{LAST} = lastPage()
	{PAGE_COUNT} = lastPage()
	{REC_COUNT} = recCount()
	{CURRENT} = currentPage()
	*/
	///////////////////////////////

	function loopPages($isLooped) {
		$this->isPagesLooped = $isLooped;
	}



//XXX old function - refactor it!
/*
	function render($allTxt, $pgTxt, $curPgTxt) 
	{
		static $placeholders = array(
			'{FIRST}','{PREVIOUS}','{PAGE_NUMBERS}','{NEXT}','{LAST}','{PAGE_COUNT}','{REC_COUNT}','{CURRENT}'
		);

		$prevPageNum = $this->currPage-1;
		if (!$prevPageNum) {
			if ($this->isPagesLooped) {
				$prevPageNum = $this->pageCount;
			} 
			else {
				$prevPageNum = 1;
			}
		}
		
		$nextPageNum = $this->currPage+1;
		if ($nextPageNum > $this->pageCount) {
			if ($this->isPagesLooped) {
				$nextPageNum = 1;
			}
			else {
				$nextPageNum = $this->pageCount;
			}
		}
		
		$txtPageNumbers = '';
		
		$pageNumsBefore = floor(($this->pageNumsVisible-1)/2);		
		$pageNumsAfter = ceil(($this->pageNumsVisible-1)/2);
		if (($this->pageCount - $this->currPage) < $pageNumsAfter) {
			$pageNumsBefore += ($pageNumsAfter - ($this->pageCount - $this->currPage));
		}
		
		$pageNum = ($this->currPage - $pageNumsBefore) <=0 ? 
							1 : 
							($this->currPage - $pageNumsBefore);
		$count = 1;
		for (; $pageNum<$this->currPage; ++$pageNum, ++$count) {
			$txtPageNumbers .= str_replace('{PAGE}', $pageNum, $pgTxt);
		}
		
		$txtPageNumbers .= str_replace('{CURRENT_PAGE}',$this->currPage,$curPgTxt);
		++$pageNum;
		
		$pageNumsAfter = $this->pageNumsVisible-$count;
		for (	$count=1; 
				$count<=$pageNumsAfter && $pageNum<=$this->pageCount; 
				++$pageNum, ++$count) {
			$txtPageNumbers .= str_replace('{PAGE}', $pageNum, $pgTxt);
		}
		
		$strValues = array(
			1,
			$prevPageNum,
			$txtPageNumbers,
			$nextPageNum,
			$this->pageCount,
			$this->pageCount,
			$this->recCount,
			$this->currPage
		);
		
		echo str_replace($placeholders, $strValues, $allTxt);
	}
*/
	
//private
	var $recOnPage;
	var $pageNumsVisible;
	var $currPage;
	var $recCount;
	var $pageCount;
	var $isPagesLooped;
	var $prevPageNum = 0;
	var $nextPageNum = 0;
	var $visiblePageNumbers = array();
};



/***************************************************************************

	Renders keywords from db table (fields 'keywords' and 'id' must be present

***************************************************************************/
class KeyWords 
{
	function __construct($tname, $idFieldName, $idValue) 
	{
		$this->tname = $tname;
		$this->fName = $idFieldName;
		$this->id = $idValue;
	}

	public function words()
	{
		global $db;
		$q = new Select($db, $this->tname);
		$q->addFields('keywords');
		$q->where($this->fName,$this->id);
		$q->run();
		$words = '';
		if ($row = $q->fetch()) {
			if (strlen($words)) {
				$words .= ',';
			}
			$words .= $row[0];
		}
		return $words;
	}
	
	public function asString()
	{

		return "<meta name='keywords' content='" . $this->words() . "'>\n";
	}
	
	
	private $tname = null;
	private $fname = null;
	private $id = null;
};


/***************************************************************************
   Menu
***************************************************************************/
class Menu
{
	public static function getItems($id)
	{
		if (!is_numeric($id)) {
			$id = query2var("SELECT id FROM cf_menu WHERE code='$id'");
		}
		return query2arrays("SELECT code,name,link FROM cf_menu_items WHERE menu_id=$id ORDER BY sort_order ASC");
	}
}


/***************************************************************************

	Block

***************************************************************************/
class Block 
{
	function __construct($id)
	{
		global $db, $lang;
		
		$q = new Select($db, 'cf_blocks');
		$q->addFields('txt');
		$q->where('lang',$lang);
		$q->where('id',$id);
		$q->run();
		$q->fetch();
		$this->txt = $q->at(0);
	}
	
	public function text() 
	{
		return $this->txt;
	}
	
	
	private $txt = '';
};



/***************************************************************************

	Elements

***************************************************************************/
class Element
{
   static public function getList($typeId, $getTxt=false)
   {
      global $lang;

      return 
         query2arrays("
            SELECT id, name, descr ".($getTxt?',txt':'')."
            FROM cf_elements
            WHERE type_id = $typeId
              AND lang = $lang
              AND enabled = 1
            ORDER BY sort_order ASC
         ");
   }

   static public function get($id)
   {
      return 
         query2array("
            SELECT id, type_id, enabled, name, descr, txt
            FROM cf_elements
            WHERE id = $id
         ");
   }

   static public function getRandom($typeId)
   {
      return
         query2array(
				limitSelect(
					"
                  SELECT id, type_id, enabled, name, descr, txt 
                  FROM cf_elements
                  WHERE type_id = $typeId
               ",
					1,
					rand(0, DB::countRecords("SELECT * FROM cf_elements WHERE type_id=$typeId")-1)
				)
			 );
    }
};

?>